package com.masterclient.app.modules.income;


import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselibrary.bean.aftersale.IncomeCommisssionFilterBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;
import com.masterclient.app.modules.income.adapter.TimeTabAdapter;

import java.util.List;


public class IncomeTimePopupWindow extends PopupWindow {
    private View mView;
    private RecyclerView recyclerView;
    List<IncomeCommisssionFilterBean> tabBeans;

    public interface CallBack{
        void dismiss();
        void OnSelect(IncomeCommisssionFilterBean tabBean);
    };

    CallBack callBack;

    public IncomeTimePopupWindow(Activity context, List<IncomeCommisssionFilterBean> tabBeans, CallBack callBack) {
        super(context);
        this.callBack = callBack;
        this.tabBeans = tabBeans;
        initView(context, tabBeans);
    }

    private void initView(final Activity context,List<IncomeCommisssionFilterBean> tabBeans) {
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = mInflater.inflate(R.layout.income_time_select_popupwindow, null);


        recyclerView = (RecyclerView) mView.findViewById(R.id.recyclerView);


        GridLayoutManager layoutManage = new GridLayoutManager(context, 3){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        recyclerView.setLayoutManager(layoutManage);
        RecyclerView.ItemDecoration decoration = new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                outRect.left = ScreenUtils.dp2px(8);
                outRect.bottom = ScreenUtils.dp2px(16);
                //由于每行都只有2个，所以第一个都是2的倍数，把左边距设为0
                if (parent.getChildLayoutPosition(view) %3 == 0) {
                    outRect.left = 0;
                }
            }
        };
        recyclerView.addItemDecoration(decoration);

        TimeTabAdapter adapter = new TimeTabAdapter();
        recyclerView.setAdapter(adapter);
        adapter.setNewData(tabBeans);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                callBack.OnSelect(tabBeans.get(position));
            }
        });


        //设置SelectPicPopupWindow的View
        this.setContentView(mView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置PopupWindow可触摸
        this.setTouchable(true);
        //设置非PopupWindow区域是否可触摸


        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        backgroundAlpha(context, 0.5f);//0.0-1.0
        this.setOnDismissListener(new OnDismissListener() {

            @Override
            public void onDismiss() {
                // TODO Auto-generated method stub
                backgroundAlpha(context, 1f);
            }
        });
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(Activity context, float bgAlpha) {
//        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
//        lp.alpha = bgAlpha;
//        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//        context.getWindow().setAttributes(lp);
    }

    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT >= 24) {

            Rect rect = new Rect();
            anchor.getWindowVisibleDisplayFrame(rect);
            Activity activity = (Activity) anchor.getContext();
            Rect outRect1 = new Rect();
            activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect1);
            int h = outRect1.height() - rect.bottom;
            setHeight(h);
        }
//        super.showAsDropDown(anchor, xoff, yoff);
//        if (Build.VERSION.SDK_INT >= 24){
//            Rect visibleFrame = new Rect();
//            anchor.getGlobalVisibleRect(visibleFrame);
//            int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
//            setHeight(height);
//        }
        super.showAsDropDown(anchor);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        callBack.dismiss();
    }
}



    //不要忘记最后写上~~~~~

//    //Dialog窗体泄露解决
//    @Override
//    public void onDestroy() {
//        super.onDestroy();
//        if (dialog != null && dialog.isShowing()) {
//
//            dialog.dismiss();
//            dialog = null;
//        }
//    }





